/* 
 * File:   StaticBody.cpp
 * Author: aljosa
 * 
 * Created on June 1, 2010, 10:40 AM
 */

#include "RectBody.h"
#include "../RenderingSystem.h"
#include "../ResourceManager.h"

#include "../MathUtil.h"

// Constructor
RectBody::RectBody(b2World *world) : Box2DNode(world) {
    texId = -1;
    border = NULL;
    fill = NULL;
}

// Destructor
RectBody::~RectBody() {


}

// Draw
void RectBody::draw(double deltaTime) {
            glPushMatrix();

            glTranslatef (WORLD_TO_SCREEN( this->body->GetXForm().position.x), WORLD_TO_SCREEN(this->body->GetXForm().position.y), 0.0f);
            glRotatef ( Rad2Deg( this->body->GetAngle() ), 0.0f, 0.0f, 1.0f );

            

            glColor3f(1.0f, 1.0f, 1.0f);

            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );

            if ( this->texId != -1 ) {
   
                glEnable(GL_TEXTURE_2D);
                glBindTexture(GL_TEXTURE_2D,  ResourceManager::getInstance().getTexture( this->texName )->texID );

            }
           // xEngine::Renderer::RenderingSystem::get()->Box2DRender.DrawSolidPolygon(((b2PolygonDef*)shapeDef)->vertices, ((b2PolygonDef*)shapeDef)->vertexCount, b2Color(0.0, 0.0, 1.0)  ); //DrawCircle( (b2CircleShape*)shape->GetLocalPosition(),


            else if ( this->fill ) {
                glColor4f( fill->getRed(), fill->getGreen(), fill->getBlue(), fill->getAlpha() );

                glEnable(GL_BLEND);
                glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
            }

            glBegin(GL_POLYGON);
            glTexCoord2f(0.0f, this->texRepeatY);
            glVertex3f ( /*this->body->GetPosition().x*/ - ((b2Vec2*)this->shapeData )->x , /*this->body->GetPosition().y + */((b2Vec2*)this->shapeData )->y , 0.0f  );

            glTexCoord2f(this->texRepeatX, this->texRepeatY);
            glVertex3f ( /*this->body->GetPosition().x +*/ ((b2Vec2*)this->shapeData )->x , /*this->body->GetPosition().y +*/ ((b2Vec2*)this->shapeData )->y , 0.0f  );

            glTexCoord2f(this->texRepeatX, 0.0f);
            glVertex3f ( /*this->body->GetPosition().x +*/ ((b2Vec2*)this->shapeData )->x ,/* this->body->GetPosition().y*/ - ((b2Vec2*)this->shapeData )->y , 0.0f  );

            glTexCoord2f(0.0f, 0.0f);
            glVertex3f ( /*this->body->GetPosition().x*/ - ((b2Vec2*)this->shapeData )->x ,/* this->body->GetPosition().y*/ - ((b2Vec2*)this->shapeData )->y , 0.0f  );
            glEnd();

           

            if ( this->fill ) {
                glColor4f( border->getRed(), border->getGreen(), border->getBlue(), border->getAlpha() );
                glBegin(GL_LINE_LOOP);
                glVertex3f (  - ((b2Vec2*)this->shapeData )->x , ((b2Vec2*)this->shapeData )->y , 0.0f  );
                glVertex3f ( ((b2Vec2*)this->shapeData )->x ,  ((b2Vec2*)this->shapeData )->y , 0.0f  );
                glVertex3f (  ((b2Vec2*)this->shapeData )->x , - ((b2Vec2*)this->shapeData )->y , 0.0f  );
                glVertex3f ( - ((b2Vec2*)this->shapeData )->x , - ((b2Vec2*)this->shapeData )->y , 0.0f  );
                glEnd();
            }

            //xEngine::Renderer::RenderingSystem::get()->Box2DRender.DrawPolygon(((b2PolygonDef*)shapeDef)->vertices, ((b2PolygonDef*)shapeDef)->vertexCount, b2Color(1.0, 0.0, 0.0)  ); //DrawCircle( (b2CircleShape*)shape->GetLocalPosition(),
            glPopMatrix();
            glDisable(GL_BLEND);
            glDisable(GL_TEXTURE_2D);

            xEngine::PlatformIndependent::SceneNode::draw(deltaTime);
}


